# -*- coding: iso-8859-1 -*-
#
#    This is part of Malvinas Argentinas Messenger
#
#    Malvinas Argentinas Messenger is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of the
#    License, or (at your option) any later version.
#
#    Malvinas Argentinas Messenger is distributed in the hope that it will be
#    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Malvinas Argentinas Messenger; if not, write to the Free
#    Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#    MA  02110-1301  USA

# Important: This file was based on emesene msnobj, and modified for
# Malvinas Argentinas Messenger.

import urllib
import base64
import sha
import os
from pymsn import p2p

class Msnobj:
	'''this class represents the msnobject, for example:
	<msnobj Creator="buddy@hotmail.com" Size="24539" Type="3" Location="TFR2C.tmp" Friendly="AAA=" SHA1D="snip" SHA1C="snip"/>
	* 2: Custom Emoticons
	* 3: Display Pictures
	* 5: Background Images
	* 7: Dynamic Display Pictures (Flash-animated DPs)
	* 8: Winks (short Flash animations)
	* 11: Voice Clips (Wave sounds)
	* 12: "SavedState" Property of Add-Ins
	* 14: MSNP15 Locations
	CUSTOM_EMOTICON = 2
	DISPLAY_PICTURE = 3
	BACKGROUND_IMAGE = 4
	DYNAMIC_DISPLAY_PICTURE = 7
	WINK = 8
	VOICE_CLIP = 11
	SAVED_STATE_PROPERTY = 12
	LOCATION = 14
	'''

	def __init__(self, account, friendly = '\x00'):
		self.creator = account
		self.type = "3"
		self.location = "picture.tmp"
		self.friendly = base64.b64encode(unicode(friendly).encode('utf-16')[2:])
		self.size = str(os.path.getsize("picture.tmp"))
		fd = open("picture.tmp")
		self.data = fd.read()
		fd.close()
		self.shaId = self.makeShaId()
		self.shaIc = self.makeShaIc()
		self.msn_object = p2p.MSNObject(self.creator, self.size, self.type, self.location, \
		self.friendly, self.shaId, self.shaIc, self.data)

	def makeShaId(self):
		return base64.b64encode(sha.new(self.data).digest())

	def makeShaIc(self):
		string = 'Creator' + self.creator
		string += 'Size' + self.size + 'Type' + self.type
		string += 'Location'+ self.location
		string += 'Friendly' + self.friendly
		string += 'SHA1D' + self.shaId
		return base64.b64encode(sha.new(string).digest())

	def __repr__(self):
		string = '<msnobj Creator="' + self.creator + '" '
		string += 'Size="' + self.size + '" Type="' + self.type + '" '
		string += 'Location="'+ self.location +'" '
		string += 'Friendly="' + self.friendly + '" '
		string += 'SHA1D="' + self.makeShaId() + '" SHA1C="' + self.makeShaIc() + '"/>'
		return string

	def quote(self):
		return urllib.quote(self.__repr__())

	#def unquote(msnobj):
	#	return urllib.unquote(msnobj)
